package cn.cjx.org.controller;

import cn.cjx.basic.exception.BusinessException;
import cn.cjx.basic.utils.AjaxResult;
import cn.cjx.basic.utils.ExcelUtil;
import cn.cjx.basic.utils.QueryResult;
import cn.cjx.basic.vo.ShopVo;
import cn.cjx.org.domain.Shop;
import cn.cjx.org.domain.ShopAuditLog;
import cn.cjx.org.query.ShopQuery;
import cn.cjx.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    @GetMapping("/echars")
    public Map<String,Object> getEchars(){
        List<ShopVo> voList = shopService.loadEcharts();
        //解析state成list
        List<Integer> x = voList.stream().map(ShopVo::getState).collect(Collectors.toList());
        //解析num成list
        List<Integer> y = voList.stream().map(ShopVo::getCountNum).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("x",x);
        map.put("y",y);
        return map;
    }

    //ecx导入
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file,1,1,Shop.class);
        System.out.println("导入数据一共【"+list.size()+"】行");
        //保存到数据库中 TODO [提示：动态sql 批量插入]

    }

    //导出
    @GetMapping("/export/excel")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.loadAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //审核通过，发送激活邮件
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return AjaxResult.success();
        }catch (BusinessException e) {//我们自己的业务错误  自定义异常
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    //审核未通过，被驳回，发送修改店铺信息邮件
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return AjaxResult.success();
        }catch (BusinessException e) {//我们自己的业务错误  自定义异常
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.success();
        }catch (BusinessException e) {//我们自己的业务错误  自定义异常
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return QueryResult 分页对象
    */
    @PostMapping("/list")
    public QueryResult<Shop> findShop(@RequestBody ShopQuery query)
    {
        QueryResult<Shop> shops = shopService.queryShops(query);
        return shops;

    }
}
